home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / FileChooserDemo.java < prev    next >
Text File  |  1998-06-30  |  15KB  |  463 lines

  1. /*
  2.  * @(#)FileChooserDemo.java    1.4 98/04/14
  3.  *
  4.  * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.preview.filechooser.*;
  23. import com.sun.java.swing.preview.*;
  24.  
  25. import java.awt.*;
  26. import java.io.File;
  27. import java.awt.event.*;
  28. import java.beans.*;
  29.  
  30.  
  31. /**
  32.  *
  33.  * A demo which makes extensive use of the file chooser.
  34.  *
  35.  * 1.4 04/14/98
  36.  * @author Jeff Dinkins
  37.  */
  38. public class FileChooserDemo extends JPanel implements ActionListener {
  39.     static JFrame frame;
  40.  
  41.     static String metal= "Metal";
  42.     static String metalClassName = "com.sun.java.swing.plaf.metal.MetalLookAndFeel";
  43.  
  44.     static String motif = "Motif";
  45.     static String motifClassName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
  46.  
  47.     static String windows = "Windows";
  48.     static String windowsClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  49.  
  50.  
  51.     JButton button;
  52.     JCheckBox useFileViewButton, accessoryButton, hiddenButton, showFullDescriptionButton;
  53.     JRadioButton noFilterButton, addFiltersButton;
  54.     JRadioButton openButton, saveButton, customButton;
  55.     JRadioButton metalButton, motifButton, windowsButton;
  56.     JRadioButton justFilesButton, justDirectoriesButton, bothFilesAndDirectoriesButton;
  57.  
  58.     JTextField customField;
  59.  
  60.     ExampleFileFilter jpgFilter, gifFilter, bothFilter;
  61.     ExampleFileView fileView;
  62.  
  63.     JPanel buttonPanel;
  64.  
  65.     public final static Dimension hpad10 = new Dimension(10,1);
  66.     public final static Dimension vpad10 = new Dimension(1,10);
  67.  
  68.     FilePreviewer previewer;
  69.     JFileChooser chooser;
  70.  
  71.     public FileChooserDemo() {
  72.     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  73.  
  74.     chooser = new JFileChooser();
  75.     previewer = new FilePreviewer(chooser);
  76.     chooser.setAccessory(previewer);
  77.  
  78.     jpgFilter = new ExampleFileFilter("jpg", "JPEG Compressed Image Files");
  79.     gifFilter = new ExampleFileFilter("gif", "GIF Image Files");
  80.     bothFilter = new ExampleFileFilter(new String[] {"jpg", "gif"}, "JPEG and GIF Image Files");
  81.  
  82.     fileView = new ExampleFileView();
  83.     fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
  84.     fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
  85.  
  86.     chooser.setAccessory(previewer);
  87.     chooser.setFileView(fileView);
  88.  
  89.     // create a radio listener to listen to option changes
  90.     OptionListener optionListener = new OptionListener();
  91.     
  92.     // Create options
  93.     openButton = new JRadioButton("Open");
  94.     openButton.setSelected(true);
  95.     openButton.addActionListener(optionListener);
  96.  
  97.     saveButton = new JRadioButton("Save");
  98.     saveButton.addActionListener(optionListener);
  99.  
  100.     customButton = new JRadioButton("Custom");
  101.     customButton.addActionListener(optionListener);
  102.  
  103.     customField = new JTextField("Doit");
  104.     customField.setAlignmentY(JComponent.TOP_ALIGNMENT);
  105.     customField.setEnabled(false);
  106.     customField.addActionListener(optionListener);
  107.  
  108.     ButtonGroup group1 = new ButtonGroup();
  109.     group1.add(openButton);
  110.     group1.add(saveButton);
  111.     group1.add(customButton);
  112.  
  113.     // filter buttons
  114.     noFilterButton = new JRadioButton("No Filtering");
  115.     noFilterButton.setSelected(true);
  116.     noFilterButton.addActionListener(optionListener);
  117.  
  118.     addFiltersButton = new JRadioButton("Add JPG and GIF Filters");
  119.     addFiltersButton.addActionListener(optionListener);
  120.  
  121.     ButtonGroup group2 = new ButtonGroup();
  122.     group2.add(noFilterButton);
  123.     group2.add(addFiltersButton);
  124.  
  125.     accessoryButton = new JCheckBox("Show Preview");
  126.     accessoryButton.addActionListener(optionListener);
  127.     accessoryButton.setSelected(true);
  128.  
  129.     // more options
  130.     hiddenButton = new JCheckBox("Show Hidden Files");
  131.     hiddenButton.addActionListener(optionListener);
  132.  
  133.     showFullDescriptionButton = new JCheckBox("Show Extensions");
  134.     showFullDescriptionButton.addActionListener(optionListener);
  135.     showFullDescriptionButton.setSelected(true);
  136.  
  137.     useFileViewButton = new JCheckBox("Use FileView");
  138.     useFileViewButton.addActionListener(optionListener);
  139.     useFileViewButton.setSelected(true);
  140.  
  141.     // File or Directory chooser options
  142.     ButtonGroup group3 = new ButtonGroup();
  143.     justFilesButton = new JRadioButton("Just Select Files");
  144.     justFilesButton.setSelected(true);
  145.     group3.add(justFilesButton);
  146.     justFilesButton.addActionListener(optionListener);
  147.  
  148.     justDirectoriesButton = new JRadioButton("Just Select Directories");
  149.     group3.add(justDirectoriesButton);
  150.     justDirectoriesButton.addActionListener(optionListener);
  151.  
  152.     bothFilesAndDirectoriesButton = new JRadioButton("Select Files or Directories");
  153.     group3.add(bothFilesAndDirectoriesButton);
  154.     bothFilesAndDirectoriesButton.addActionListener(optionListener);
  155.  
  156.     // Create show button
  157.     button = new JButton("Show FileChooser");
  158.     button.addActionListener(this);
  159.         button.setMnemonic('s'); 
  160.  
  161.     // Create laf buttons.
  162.     metalButton = new JRadioButton(metal);
  163.         metalButton.setMnemonic('o'); 
  164.     metalButton.setActionCommand(metalClassName);
  165.  
  166.     motifButton = new JRadioButton(motif);
  167.         motifButton.setMnemonic('m'); 
  168.     motifButton.setActionCommand(motifClassName);
  169.  
  170.     windowsButton = new JRadioButton(windows);
  171.         windowsButton.setMnemonic('w'); 
  172.     windowsButton.setActionCommand(windowsClassName);
  173.  
  174.     ButtonGroup group4 = new ButtonGroup();
  175.     group4.add(metalButton);
  176.     group4.add(motifButton);
  177.     group4.add(windowsButton);
  178.  
  179.         // Register a listener for the laf buttons.
  180.     metalButton.addActionListener(optionListener);
  181.     motifButton.addActionListener(optionListener);
  182.     windowsButton.addActionListener(optionListener);
  183.  
  184.     JPanel control1 = new JPanel();
  185.     control1.setLayout(new BoxLayout(control1, BoxLayout.X_AXIS));
  186.     control1.add(Box.createRigidArea(hpad10));
  187.     control1.add(openButton); 
  188.     control1.add(Box.createRigidArea(hpad10));
  189.     control1.add(saveButton);
  190.     control1.add(Box.createRigidArea(hpad10));
  191.     control1.add(customButton);
  192.     control1.add(customField); 
  193.     control1.add(Box.createRigidArea(hpad10));
  194.  
  195.     JPanel control2 = new JPanel();
  196.     control2.setLayout(new BoxLayout(control2, BoxLayout.X_AXIS));
  197.     control2.add(Box.createRigidArea(hpad10));
  198.     control2.add(noFilterButton);
  199.     control2.add(Box.createRigidArea(hpad10));
  200.     control2.add(addFiltersButton);
  201.     control2.add(Box.createRigidArea(hpad10));
  202.     control2.add(accessoryButton);
  203.     control2.add(Box.createRigidArea(hpad10));
  204.  
  205.     JPanel control3 = new JPanel();
  206.     control3.setLayout(new BoxLayout(control3, BoxLayout.X_AXIS));
  207.     control3.add(Box.createRigidArea(hpad10));
  208.     control3.add(hiddenButton);
  209.     control3.add(Box.createRigidArea(hpad10));
  210.     control3.add(showFullDescriptionButton);
  211.     control3.add(Box.createRigidArea(hpad10));
  212.     control3.add(useFileViewButton);
  213.     control3.add(Box.createRigidArea(hpad10));
  214.  
  215.     JPanel control4 = new JPanel();
  216.     control4.setLayout(new BoxLayout(control4, BoxLayout.X_AXIS));
  217.     control4.add(Box.createRigidArea(hpad10));
  218.     control4.add(justFilesButton);
  219.     control4.add(Box.createRigidArea(hpad10));
  220.     control4.add(justDirectoriesButton);
  221.     control4.add(Box.createRigidArea(hpad10));
  222.     control4.add(bothFilesAndDirectoriesButton);
  223.     control4.add(Box.createRigidArea(hpad10));
  224.  
  225.     JPanel panel = new JPanel();
  226.     panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
  227.     panel.add(Box.createRigidArea(hpad10));
  228.     panel.add(button);        
  229.     panel.add(Box.createRigidArea(hpad10));
  230.     panel.add(metalButton);   
  231.     panel.add(Box.createRigidArea(hpad10));
  232.     panel.add(motifButton);   
  233.     panel.add(Box.createRigidArea(hpad10));
  234.     panel.add(windowsButton); 
  235.     panel.add(Box.createRigidArea(hpad10));
  236.  
  237.     add(Box.createRigidArea(vpad10));
  238.     add(control1);
  239.     add(Box.createRigidArea(vpad10));
  240.     add(control2);
  241.     add(Box.createRigidArea(vpad10));
  242.     add(control3);
  243.     add(Box.createRigidArea(vpad10));
  244.     add(control4);
  245.     add(Box.createRigidArea(vpad10));
  246.     add(Box.createRigidArea(vpad10));
  247.     add(panel);
  248.     add(Box.createRigidArea(vpad10));
  249.     }
  250.  
  251.     public void actionPerformed(ActionEvent e) {
  252.     int retval = chooser.showDialog(frame, null);
  253.     if(retval == JFileChooser.APPROVE_OPTION) {
  254.         File theFile = chooser.getSelectedFile();
  255.         if(theFile != null) {
  256.         if(theFile.isDirectory()) {
  257.             JOptionPane.showMessageDialog(
  258.             frame, "You chose this directory: " +
  259.             chooser.getSelectedFile().getAbsolutePath()
  260.             );
  261.         } else {
  262.             JOptionPane.showMessageDialog(
  263.             frame, "You chose this file: " +
  264.             chooser.getSelectedFile().getAbsolutePath()
  265.             );
  266.         }
  267.         return;
  268.         }
  269.     }
  270.     JOptionPane.showMessageDialog(frame, "No file was chosen.");
  271.     }
  272.  
  273.     /** An ActionListener that listens to the radio buttons. */
  274.     class OptionListener implements ActionListener {
  275.     public void actionPerformed(ActionEvent e) {
  276.         JComponent c = (JComponent) e.getSource();
  277.         if(c == openButton) {
  278.         chooser.setDialogType(JFileChooser.OPEN_DIALOG);
  279.         customField.setEnabled(false);
  280.         repaint();
  281.         } else if (c == saveButton) {
  282.         chooser.setDialogType(JFileChooser.SAVE_DIALOG);
  283.         customField.setEnabled(false);
  284.         repaint();
  285.         } else if (c == customButton || c == customField) {
  286.         customField.setEnabled(true);
  287.         chooser.setApproveButtonText(customField.getText());
  288.         repaint();
  289.         } else if(c == noFilterButton) {
  290.         chooser.resetChoosableFileFilters();
  291.         } else if(c == addFiltersButton) {
  292.         chooser.addChoosableFileFilter(bothFilter);
  293.         chooser.addChoosableFileFilter(jpgFilter);
  294.         chooser.addChoosableFileFilter(gifFilter);
  295.         } else if(c == hiddenButton) {
  296.         chooser.setFileHidingEnabled(!hiddenButton.isSelected());
  297.         } else if(c == accessoryButton) {
  298.         if(accessoryButton.isSelected()) {
  299.             chooser.setAccessory(previewer);
  300.         } else {
  301.             chooser.setAccessory(null);
  302.         }
  303.         } else if(c == useFileViewButton) {
  304.         if(useFileViewButton.isSelected()) {
  305.             chooser.setFileView(fileView);
  306.         } else {
  307.             chooser.setFileView(null);
  308.         }
  309.         } else if(c == showFullDescriptionButton) {
  310.         jpgFilter.setExtensionListInDescription(showFullDescriptionButton.isSelected());
  311.         gifFilter.setExtensionListInDescription(showFullDescriptionButton.isSelected());
  312.         bothFilter.setExtensionListInDescription(showFullDescriptionButton.isSelected());
  313.         if(addFiltersButton.isSelected()) {
  314.             chooser.resetChoosableFileFilters();
  315.             chooser.addChoosableFileFilter(bothFilter);
  316.             chooser.addChoosableFileFilter(jpgFilter);
  317.             chooser.setFileFilter(gifFilter);
  318.         }
  319.         } else if(c == justFilesButton) {
  320.         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  321.         } else if(c == justDirectoriesButton) {
  322.         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  323.         } else if(c == bothFilesAndDirectoriesButton) {
  324.         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  325.         } else {
  326.         String lnfName = e.getActionCommand();
  327.         
  328.         try {
  329.             UIManager.setLookAndFeel(lnfName);
  330.             SwingUtilities.updateComponentTreeUI(frame);
  331.             if(chooser != null) {
  332.             SwingUtilities.updateComponentTreeUI(chooser);
  333.             }
  334.             frame.pack();
  335.         } catch (UnsupportedLookAndFeelException exc) {
  336.             System.out.println("Unsupported L&F Error:" + exc);
  337.             JRadioButton button = (JRadioButton)e.getSource();
  338.             button.setEnabled(false);
  339.             updateState();
  340.         } catch (IllegalAccessException exc) {
  341.             System.out.println("IllegalAccessException Error:" + exc);
  342.         } catch (ClassNotFoundException exc) {
  343.             System.out.println("ClassNotFoundException Error:" + exc);
  344.         } catch (InstantiationException exc) {
  345.             System.out.println("InstantiateException Error:" + exc);
  346.         }
  347.         }
  348.         
  349.     }
  350.     }
  351.  
  352.     public void updateState() {
  353.     String lnfName = UIManager.getLookAndFeel().getClass().getName();
  354.     if (lnfName.indexOf(metal) >= 0) {
  355.         metalButton.setSelected(true);
  356.     } else if (lnfName.indexOf(windows) >= 0) {
  357.         windowsButton.setSelected(true);
  358.     } else if (lnfName.indexOf(motif) >= 0) {
  359.         motifButton.setSelected(true);
  360.     } else {
  361.         System.err.println("FileChooserDemo if using an unknown L&F: " + lnfName);
  362.     }
  363.     }
  364.  
  365.     class FilePreviewer extends JComponent implements PropertyChangeListener {
  366.     ImageIcon thumbnail = null;
  367.     File f = null;
  368.  
  369.     public FilePreviewer(JFileChooser fc) {
  370.         setPreferredSize(new Dimension(100, 50));
  371.         fc.addPropertyChangeListener(this);
  372.     }
  373.  
  374.     public void loadImage() {
  375.         if(f != null) {
  376.         ImageIcon tmpIcon = new ImageIcon(f.getPath());
  377.         if(tmpIcon.getIconWidth() > 90) {
  378.             thumbnail = new ImageIcon(
  379.             tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
  380.         } else {
  381.             thumbnail = tmpIcon;
  382.         }
  383.         }
  384.     }
  385.  
  386.     public void propertyChange(PropertyChangeEvent e) {
  387.         String prop = e.getPropertyName();
  388.         if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
  389.         f = (File) e.getNewValue();
  390.         if(isShowing()) {
  391.             loadImage();
  392.             repaint();
  393.         }
  394.         }
  395.     }
  396.  
  397.     public void paint(Graphics g) {
  398.         if(thumbnail == null) {
  399.         loadImage();
  400.         }
  401.         if(thumbnail != null) {
  402.         int x = getWidth()/2 - thumbnail.getIconWidth()/2;
  403.         int y = getHeight()/2 - thumbnail.getIconHeight()/2;
  404.         if(y < 0) {
  405.             y = 0;
  406.         }
  407.  
  408.         if(x < 5) {
  409.             x = 5;
  410.         }
  411.         thumbnail.paintIcon(this, g, x, y);
  412.         }
  413.     }
  414.     }
  415.  
  416.     public static void main(String s[]) {
  417.     /* 
  418.        NOTE: By default, the look and feel will be set to the
  419.        Cross Platform Look and Feel (which is currently Metal).
  420.        The user may someday be able to override the default
  421.        via a system property. If you as the developer want to
  422.        be sure that a particular L&F is set, you can do so
  423.        by calling UIManager.setLookAndFeel(). For example, the
  424.        first code snippet below forcibly sets the UI to be the
  425.        System Look and Feel. The second code snippet forcibly
  426.        sets the look and feel to the Cross Platform L&F.
  427.  
  428.        Snippet 1:
  429.        try {
  430.           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  431.        } catch (Exception exc) {
  432.           System.err.println("Error loading L&F: " + exc);
  433.        }
  434.  
  435.        Snippet 2:
  436.        try {
  437.           UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  438.        } catch (Exception exc) {
  439.           System.err.println("Error loading L&F: " + exc);
  440.        }
  441.     */
  442.  
  443.     try {
  444.         // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  445.         UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  446.     } catch (Exception exc) {
  447.         System.err.println("Error loading L&F: " + exc);
  448.     }
  449.  
  450.     FileChooserDemo panel = new FileChooserDemo();
  451.     
  452.     frame = new JFrame("FileChooserDemo");
  453.     frame.addWindowListener(new WindowAdapter() {
  454.         public void windowClosing(WindowEvent e) {System.exit(0);}
  455.     });
  456.     frame.getContentPane().add("Center", panel);
  457.     frame.pack();
  458.     frame.setVisible(true);
  459.     
  460.     panel.updateState();
  461.     }
  462. }
  463.